package com.example.gulimall.product.service.impl;

import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.example.gulimall.product.dao.BrandDao;
import com.example.gulimall.product.dao.CategoryDao;
import com.example.gulimall.product.entity.BrandEntity;
import com.example.gulimall.product.entity.CategoryEntity;
import com.example.gulimall.product.vo.RelationBrandListVo;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.Locale;
import java.util.Map;
import java.util.stream.Collectors;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.example.common.utils.PageUtils;
import com.example.common.utils.Query;

import com.example.gulimall.product.dao.CategoryBrandRelationDao;
import com.example.gulimall.product.entity.CategoryBrandRelationEntity;
import com.example.gulimall.product.service.CategoryBrandRelationService;


@Service("categoryBrandRelationService")
public class CategoryBrandRelationServiceImpl extends ServiceImpl<CategoryBrandRelationDao, CategoryBrandRelationEntity> implements CategoryBrandRelationService {

    @Autowired
    private BrandDao brandDao;
    @Autowired
    private CategoryDao categoryDao;

    @Override
    public List<CategoryBrandRelationEntity> queryPage(Long brandId) {
        return this.baseMapper.selectList(
                Wrappers.<CategoryBrandRelationEntity>lambdaQuery()
                        .eq(CategoryBrandRelationEntity::getBrandId, brandId)
        );
    }

    @Override
    public void saveDetails(CategoryBrandRelationEntity relation) {
        relation.setBrandName(this.brandDao.selectById(relation.getBrandId()).getName());
        relation.setCatelogName(this.categoryDao.selectById(relation.getCatelogId()).getName());
        this.save(relation);
    }

    @Override
    public void updateBrand(BrandEntity brand) {
        CategoryBrandRelationEntity entity = new CategoryBrandRelationEntity();
        entity.setBrandName(brand.getName());
        entity.setBrandId(brand.getBrandId());

        this.baseMapper.update(entity, Wrappers.<CategoryBrandRelationEntity>lambdaUpdate()
                .eq(CategoryBrandRelationEntity::getBrandId, brand.getBrandId()));
    }

    @Override
    public void updateCategory(CategoryEntity category) {
        CategoryBrandRelationEntity entity = new CategoryBrandRelationEntity();
        entity.setCatelogName(category.getName());
        entity.setCatelogId(category.getCatId());

        this.baseMapper.update(entity, Wrappers.<CategoryBrandRelationEntity>lambdaUpdate()
                .eq(CategoryBrandRelationEntity::getCatelogId, category.getCatId()));
    }

    @Override
    public List<RelationBrandListVo> relationBrandList(Long catId) {
        List<CategoryBrandRelationEntity> entityList = this.baseMapper.selectList(
                Wrappers.<CategoryBrandRelationEntity>lambdaQuery()
                        .eq(CategoryBrandRelationEntity::getCatelogId, catId)
        );

        return entityList.stream().map(
                a -> {
                    RelationBrandListVo vo = new RelationBrandListVo();
                    BeanUtils.copyProperties(a, vo);
                    return vo;
                }
        ).collect(Collectors.toList());
    }
}